//
//  ListOfShop.m
//  PetAPP
//
//  Created by apple on 16/2/18.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "ListOfShop.h"
#import "ModelOfShopForPet.h"
#import "NetworkingHandle.h"
#import "CellOfShopForPet.h"
#import "ModelOfShopForPet.h"

@interface ListOfShop () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UISegmentedControl *sg;

@property (nonatomic, retain) UIView *viewOfDog;

@property (nonatomic, retain) UIView *viewOfCat;

@property (nonatomic, retain) UICollectionView *collectionOfDog;

@property (nonatomic, retain) UICollectionView *collectionOfCat;

@property (nonatomic, retain) NSMutableArray *dataArrForDog;

@property (nonatomic, retain) NSMutableArray *dataArrForCat;

@end

@implementation ListOfShop

- (void)dealloc {

    [_sg release];
    [super dealloc];
}

- (void)viewDidLoad {

    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"商品分类";
    [self createBannerView];
    [self createSubViews];
    [self handleData];


}

- (void)handleData {
    /** 狗狗商品数据 */
    [NetworkingHandle handleWorkData:@"http://ts.leepet.com/plugins/API.v1.0/?&a=goods&m=category" AndBody:@"page=1&pageSize=10&parentID=100" value:@{@"Content-Type" : @"application/x-www-form-urlencoded"} handleBlock:^(id result) {
        
        self.dataArrForDog = [NSMutableArray array];
        
        NSArray *tempArr = [result objectForKey:@"data"];
        for (NSDictionary *dic in tempArr) {
            
            ModelOfShopForPet *model = [[ModelOfShopForPet alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArrForDog addObject:model];
            [model release];
        }
        
        [self.collectionOfDog reloadData];
        
        
    }];
    
    /** 猫猫商品数据 */
    [NetworkingHandle handleWorkData:@"http://ts.leepet.com/plugins/API.v1.0/?&a=goods&m=category" AndBody:@"page=1&pageSize=10&parentID=99" value:@{@"Content-Type" : @"application/x-www-form-urlencoded"} handleBlock:^(id result) {
        
        self.dataArrForCat = [NSMutableArray array];
        
        NSArray *tempArr = [result objectForKey:@"data"];
        for (NSDictionary *dic in tempArr) {
            
            ModelOfShopForPet *model = [[ModelOfShopForPet alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArrForCat addObject:model];
            [model release];
        }
        
        [self.collectionOfCat reloadData];
        
    }];
    
   

    
    
}

- (void)createBannerView {
    UIView *viewOfBanner = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 60)];
    [self.view addSubview:viewOfBanner];
    [viewOfBanner release];
    
    viewOfBanner.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:0.9231 green:0.9231 blue:0.9231 alpha:1.0], [UIColor grayColor]);
    
    self.sg = [[UISegmentedControl alloc] initWithItems:@[@"汪星人", @"喵星人"]];
    [viewOfBanner addSubview:_sg];
    [_sg release];
    
    self.sg.frame = CGRectMake(self.view.frame.size.width / 3.0f / 2, 10, self.view.frame.size.width / 3.0f * 2, 40);
    
    self.sg.selectedSegmentIndex = 0;
//    self.sg.backgroundColor = [UIColor colorWithRed:255/256.0 green:207/256.0 blue:87/256.0 alpha:1];
    self.sg.backgroundColor = [UIColor whiteColor];
    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName: [UIColor colorWithRed:0.2784 green:0.0 blue:0.0667 alpha:1.0]};
    [self.sg setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
    
    
    /** 设置字体非选中时颜色 */
    NSDictionary *unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],NSForegroundColorAttributeName: [UIColor colorWithRed:255/256.0 green:207/256.0 blue:87/256.0 alpha:1]};
    [self.sg setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    [self.sg setTintColor:[UIColor colorWithRed:255/256.0 green:207/256.0 blue:87/256.0 alpha:1]];
    
    [self.sg addTarget:self action:@selector(handleChangeView:) forControlEvents:UIControlEventValueChanged];

}

- (void)handleChangeView:(UISegmentedControl *)sg {
    if (sg.selectedSegmentIndex == 0) {
        [self.view bringSubviewToFront:self.viewOfDog];
    }else {
        [self.view bringSubviewToFront:self.viewOfCat];
    }

}

- (void)createSubViews {

    self.viewOfCat = [[UIView alloc] initWithFrame:CGRectMake(0, 124, self.view.frame.size.width, self.view.frame.size.height - 124)];
    [self.view addSubview:_viewOfCat];
    [_viewOfCat release];
    
    self.viewOfCat.backgroundColor = [UIColor blueColor];
    

    self.viewOfDog = [[UIView alloc] initWithFrame:CGRectMake(0, 124, self.view.frame.size.width, self.view.frame.size.height - 124)];
    [self.view addSubview:_viewOfDog];
    [_viewOfDog release];
    
    self.viewOfDog.backgroundColor = [UIColor redColor];
    
    [self createCollectionView];
    
    
}

- (void)createCollectionView {

    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 4 * 20) / 3, 150);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 20;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    
    
    
    
    self.collectionOfDog = [[UICollectionView alloc] initWithFrame:(CGRect){.size = self.viewOfDog.frame.size} collectionViewLayout:flowLayout];
    [self.viewOfDog addSubview:_collectionOfDog];
    [_collectionOfDog release];
    self.collectionOfDog.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.2952 green:0.2952 blue:0.2952 alpha:1.0]);

    self.collectionOfDog.delegate = self;
    self.collectionOfDog.dataSource = self;
    self.collectionOfDog.tag = 1001;
    
    [self.collectionOfDog registerClass:[CellOfShopForPet class] forCellWithReuseIdentifier:@"poolOfCell"];
    
    
    self.collectionOfCat = [[UICollectionView alloc] initWithFrame:(CGRect){.size = self.viewOfDog.frame.size} collectionViewLayout:flowLayout];
    [self.viewOfCat addSubview:_collectionOfCat];
    [_collectionOfCat release];
    self.collectionOfCat.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.2952 green:0.2952 blue:0.2952 alpha:1.0]);
    self.collectionOfCat.delegate = self;
    self.collectionOfCat.dataSource = self;
    
    [self.collectionOfCat registerClass:[CellOfShopForPet class] forCellWithReuseIdentifier:@"poolOfCell"];
    
    self.collectionOfCat.tag = 1002;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArrForDog.count;

    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CellOfShopForPet *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolOfCell" forIndexPath:indexPath];
    ModelOfShopForPet *model = nil;
    if (collectionView.tag == 1001) {
        model = [self.dataArrForDog objectAtIndex:indexPath.item];
    }else {
        model = [self.dataArrForCat objectAtIndex:indexPath.item];
    
    }
    
    [cell handleDataWithModel:model];
   
    
    
    cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.2952 green:0.2952 blue:0.2952 alpha:1.0]);
    
    return cell;
}


@end
